/**************************************************************
 * fetchlyric_localdir.c - 
 * Copyright (c) 2009  Qin Xiaoxue  <crs.chin@gmail.com>
 *
 * This file is  free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
**************************************************************/


#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>

#include "common.h"
#include "fetchlyric.h"
#include "configfile.h"

#define LOCALDIR_DIR ".lyrics"

/* static GSList   *localdir_fetchlyric_get_list(struct fetchlyric_plugin *plugin, const gchar *title, const gchar *artist) */
/* { */
/* } */

/* static FILE      *localdir_fetchlyric_get_from_url(struct fetchlyric_plugin, void *url) */
/* { */
/* } */


/* static GSList    *localdir_fetchlyric_free_list(struct fetchlyric_plugin *plugin, GSList *list) */
/* { */

/* } */

struct localdir_struct{
  const gchar *plugin;
  const gchar *strerr;
  gboolean err;
  gboolean initialized;
};

static struct localdir_struct plugin_data = {
  .plugin = "localdir",
  .strerr = NULL,
  .err = FALSE,
  .initialized = FALSE,
};


#define LYRIC_DIR ".Lyric"

static gchar *get_datadir(const gchar *basedir, const gchar *category)
{
  const gchar *_basedir, *_category;
  gchar *path;

  if(basedir)
	_basedir = basedir;
  else
	_basedir = g_getenv("HOME");

  if(! _basedir)
	_basedir = "/tmp";

  if(category)
	_category = category;
  else
	_category = LYRIC_DIR;

  path = g_strdup_printf("%s/%s", _basedir, _category);
  if(! g_mkdir_with_parents(path, 0755))
	return path;

  g_free((void *)path);
  return NULL;
}

FILE *get_data(const gchar *basedir, const gchar *category, const gchar *fname)
{
  gchar *dir, *path;
  FILE *ret;

  if(! fname)
	return NULL;
  dir = get_datadir(basedir, category);
  if(! dir)
	return NULL;

  path = g_strdup_printf("%s/%s", dir, fname);

  ret = fopen(path, "r");
  g_free(dir);
  g_free(path);

  return ret;

}

static FILE *__localdir_search(const gchar *title)
{
  DIR *dir;
  FILE *ret = NULL;
  struct dirent *ent;
  gint len = strlen(title);
  gchar *path  = get_datadir(NULL, LOCALDIR_DIR);

  if(! path)
    return NULL;

  dir = opendir(path);
  if(! dir)  {
    g_free(path);
    return NULL;
  }

  while(ent = readdir(dir))
    if(! strncasecmp(ent->d_name, title, len))
      break;

  if(ent)
    ret = get_data(NULL, LOCALDIR_DIR, ent->d_name);

  closedir(dir);

  return ret;
}


static FILE     *localdir_fetchlyric_get(struct fetchlyric_plugin *plugin, const gchar *title, const gchar *artist)
{
  gchar *fname;
  FILE *res;

  if(! title)  {
    plugin_data.err = TRUE;
    plugin_data.strerr = "** title is mandatory.";
    return NULL;
  }

  if(artist)  {
    fname = g_strdup_printf("%s - %s.lrc", title, artist ? : "");
    res = get_data(NULL, LOCALDIR_DIR, fname);
    g_free(fname);
    return res;
  }

  return __localdir_search(title);
}


static gboolean     localdir_is_error(struct fetchlyric_plugin *plugin)
{
  return plugin_data.err;
}


static const gchar *localdir_strerror(struct fetchlyric_plugin *plugin)
{
  if(plugin_data.err)
    return plugin_data.strerr;

  return NULL;
}


static struct fetchlyric_plugin  *localdir_init(int argc, char **argv)
{
  struct fetchlyric_plugin *plugin;
  gchar *dir;

  dir = get_datadir(NULL, LOCALDIR_DIR);
  g_free(dir);

  plugin = g_new0(struct fetchlyric_plugin, 1);

/*   plugin->fetchlyric_get_list = localdir_fetchlyric_get_list; */
/*   plugin->fetchlyric_get_from_url = localdir_fetchlyric_get_from_url; */
/*   plugin->fetchlyric_free_list = localdir_fetchlyric_free_list; */
  plugin->fetchlyric_get = localdir_fetchlyric_get;
  plugin->is_error = localdir_is_error;
  plugin->strerror = localdir_strerror;

  plugin_data.initialized = TRUE;
  plugin->data = &plugin_data;

  return plugin;
}


static  void                       localdir_exit(struct fetchlyric_plugin *plugin)
{
  if(plugin->data != &plugin_data)
    return;

  g_free(plugin);
}


struct fetchlyric_plugin_descrip FETCHLYRIC_EXPORT_SYMBOL = {
  .init = localdir_init,
  .exit = localdir_exit,

  .name = "localdir",
  .description = "load lyrics from specific local dir.",
  .author = "X Q",
  .mail = "crs.chin@gmail.com",
  .version = "0.1",
  .islocal = TRUE
};


//////////////////////////////TESTING CODE//////////////////////////////////////

#if defined(TEST_FETCHLYRIC_LOCALDIR)

ing main(int argc, char **argv)
{

}


#endif
